package com.多用户通讯编程.QQ.Client.View;

import com.多用户通讯编程.QQ.Client.Service.UserClientService;
import com.多用户通讯编程.QQ.Common.Message;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Scanner;

//菜单；
public class View {
    static Scanner sc = new Scanner(System.in);
    String key;              /// 选择选项
    boolean loop1 = true;    ///一级菜单控制布尔值
    boolean loop2 = true;   ///二级菜单控制布尔值
    String userId;
    String password;
    /// 用这个对象的check方法来 判断用户和密码是否正确；
    UserClientService userClientService = new UserClientService();
//    static ObjectOutputStream oos = null;
//    public static void setOos(ObjectOutputStream o){
//        oos = o;
//    }


    void mainView_1() {
        while (loop1) {
            System.out.println("=========网络通讯一级菜单==========");
            System.out.println("\t\t1 登录系统");
            System.out.println("\t\t9 退出系统");
            System.out.println("========================");
            System.out.println("请选择您的选择 ");
            key = sc.next();
            switch (key) {
                case "1":
                    System.out.print("您的用户名:");
                    userId = sc.next();
                    System.out.print("您的密码  :");
                    password = sc.next();

                    /**
                     这里要验证账户密码受否正确；
                     1.UserClientService
                     打包成User对象发送给服务端，再接受服务端的验证回复，
                     如果消息类型是成功，就启动读取服务端信息的线程ClientConnectServerThread
                     2.ClientConnectServerThread
                     创建一个线程对象。将当前客户端的socket传进去，用来跟服务端通讯；
                     */
                    /// 如果验证不成功就不进入二级菜单
                    if(!userClientService.checkUser(userId,password)) {
                        System.out.println("登陆失败");
                        break;
                    }

                    /// 二级菜单
                    mainView_2();
                    break;
                case "9":
                    loop1 = false;
                    break;
                default:
            }
        }
    }

    void mainView_2() {
        System.out.println("欢迎用户  (" + userId + ")  登录");
        /// 防止退出登录后再次登录，loop2改成false，第二次进不去；
        loop2 = true;
        while (loop2) {
            try {
                Thread.sleep(200); ///防止二级菜单刷新太快
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("==========网络通讯二级菜单=========");
            System.out.println("\t\t1 显示在线用户列表");
            System.out.println("\t\t2 群发消息");
            System.out.println("\t\t3 私聊消息");
            System.out.println("\t\t4 发送文件");
            System.out.println("\t\t9 退出系统");
            System.out.println("========================");
            System.out.println("请选择序号~ ");
            key = sc.next();
            switch (key) {
                case "1":
                    userClientService.getUsers();
                    break;
                case "2":
                userClientService.ClientGroupMessaging();
                    break;
                case "3":
                    userClientService.SendMessagepPrivately();

                    break;
                case "4":
                    userClientService.sendFile();
                    break;
                case "9":
                    loop2 = false;
                    userClientService.ClientThreadExit();
                    break;
                default:
            }
        }

    }

    public static void main(String[] args) {
        new View().mainView_1();
    }
}
